﻿<%@ WebHandler Language="C#" Class="OrganizationTreeCombo" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using Chenxin.Lib.Utility;
using System.Data;

public class OrganizationTreeCombo : AjaxHandlerBase {

    public override void RequestProcess(HttpContext context)
    {
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        if (context.Request["op"] != null)
        {
            switch (context.Request["op"].ToLower())
            {
                case "gettree":
                    GetTree(context);
                    break;
                case "gettext":
                    GetText(context);
                    break;
                case "getdepartuser":
                    GetDepartUser(context);
                    break;
            }
        }
    }

    /// <summary>
    /// 获取Text显示值
    /// </summary>
    /// <param name="context"></param>
    public void GetText(HttpContext context)
    {
        try
        {
            switch (context.Request["txttype"].ToLower())
            {
                case "depart":
                    Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo dmodel = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(new Guid(context.Request["val"]));

                    if (dmodel != null)
                    {
                        context.Response.Write(string.Format("{{success:1,msg:'',data:{{txt:'{0}'}}}}", dmodel.DepartName));
                    }
                    else
                    {
                        context.Response.Write(string.Format("{{success:0,msg:'获取显示值失败！',data:''}}", ""));
                    }
                    break;
                case "user":
                    Chenxin.Lib.Passport.Entity.Hr_UserInfo umodel = Chenxin.Lib.Passport.DAL.Hr_UserInfo.GetEntityById(context.Request["val"]);

                    if (umodel != null)
                    {
                        context.Response.Write(string.Format("{{success:1,msg:'',data:{{txt:'{0}'}}}}", umodel.UserName));
                    }
                    else
                    {
                        context.Response.Write(string.Format("{{success:0,msg:'获取显示值失败！',data:''}}", ""));
                    }
                    break;
                default:
                    context.Response.Write(string.Format("{{success:0,msg:'获取显示值失败！',data:''}}", ""));
                    break;
            }
        }
        catch
        {
            context.Response.Write(string.Format("{{success:0,msg:'获取显示值失败！',data:''}}", ""));
        }
    }

    /// <summary>
    /// 获取树
    /// </summary>
    /// <param name="context"></param>
    public void GetTree(HttpContext context)
    {
        try
        {
            string fatherid = context.Request.Params["fatherid"];
            string type = context.Request.Params["type"];
            System.Text.StringBuilder str = new System.Text.StringBuilder();

            if (type == "500" || type == "600")
            {
                str.AppendFormat(" BelongDepartId='{0}'", fatherid);

                string status = "1";
                if (context.Request["status"] != null)
                {
                    status = context.Request["status"];
                }
                str.AppendFormat(" and Status={0}", (status == "0") ? "-1" : status);
            }
            else
            {
                //查询权限判断        
                if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllNode))//查看所有节点权限
                {
                    str.Append(" (1=1) ");
                }
                else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityNode))//查看本城市节点权限
                {
                    str.AppendFormat(" cityid='{0}' ", CurrentUserInfo.CityId);
                }
                else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCompanyNode))//查看本公司节点权限
                {
                    if (type != "0")
                    {
                        str.AppendFormat(" companyid='{0}' ", CurrentUserInfo.CompanyId);
                    }
                    else
                    {
                        str.AppendFormat(" cityid='{0}' ", CurrentUserInfo.CityId);
                    }
                }
                else//没有权限
                {
                    str.Append(" (1=0) ");
                }
                str.AppendFormat(" and ParentId='{0}' ", fatherid);
            }


            //权限判断结束

            DataSet ds = Chenxin.Lib.Hr.BLL.OrganizationManage.GetTreeList(str.ToString(), Convert.ToInt32(type));
            StringBuilder nodeSb = new StringBuilder();
            foreach (DataRow dr in ds.Tables[0].Select("1=1"))
            {
                nodeSb.AppendFormat("{{val:'{0}',txt:'{1}',type:'{2}',code:'{3}',cityid:'{4}',cityname:'{5}',companyid:'{6}',companyname:'{7}'}},", dr["val"], dr["txt"], dr["type"], dr["code"], dr["cityid"], dr["city"], dr["companyid"], dr["company"]);
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                nodeSb.Remove(nodeSb.Length - 1, 1);
            }
            context.Response.Write(string.Format("{{success:1,msg:'',data:{{items:[{0}]}}}}", nodeSb.ToString()));
        }
        catch (Exception ex)
        {
            context.Response.Write(string.Format("{{success:'0',msg:'意外错误：{0}',data:''}}", ex.Message));
        }

    }


    /// <summary>
    /// 获取部门人员
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    private void GetDepartUser(HttpContext context)
    {
        try
        {
            string fatherid = context.Request.Params["fatherid"];
            System.Text.StringBuilder str = new System.Text.StringBuilder();

            str.AppendFormat(" BelongDepartId='{0}'", fatherid);

            string status = "1";
            if (context.Request["status"] != null)
            {
                status = context.Request["status"];
            }
            str.AppendFormat(" and Status={0}", status);

            //权限判断结束

            DataSet ds = Chenxin.Lib.Hr.BLL.OrganizationManage.GetTreeList(str.ToString(), 500);
            StringBuilder nodeSb = new StringBuilder();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                nodeSb.AppendFormat("{{val:'{0}',txt:'{1}',type:'{2}',cityid:'{3}',cityname:'{4}',companyid:'{5}',companyname:'{6}',code:'{7}'}},",
                    dr["val"], dr["txt"], dr["type"], dr["cityid"], dr["city"], dr["companyid"], dr["company"], dr["code"]);
            }
            if (ds.Tables[0].Rows.Count > 0)
            {
                nodeSb.Remove(nodeSb.Length - 1, 1);
            }
            context.Response.Write(string.Format("{{success:1,msg:'',data:{{items:[{0}]}}}}", nodeSb.ToString()));
        }
        catch (Exception ex)
        {
            context.Response.Write(string.Format("{{success:'0',msg:'意外错误：{0}',data:''}}", ex.Message));
        }
    }
}